import pytest
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep
from faker import Faker
faker = Faker(locale='zh_cn')
name = faker.name()

base_url = "http://121.43.29.41:8899/admin/login"
class TestDemo4:
    def setup_method(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()

    def teardown_method(self):
        sleep(10)
        self.driver.quit()  # 退出浏览器

    def test_crmeb(self):
        self.driver.get(base_url)
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'el-input__inner')))[0].send_keys("admin")
        sleep(2)
        #输入密码
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,'el-input__inner')))[1].send_keys("admin123.")

        #点击登录按钮
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((By.CLASS_NAME,'el-button'))).click()
        #等待页面主页加载
        sleep(5)


        #点击用户tap
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,'font12')))[1].click()

        #点击用户分组
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((By.XPATH,'//*[text()="用户分组"]'))).click()
        sleep(2)

        #点击添加分组
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((By.XPATH,'//*[text()="添加分组"]'))).click()
        sleep(1)

        #输入分组名称
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'el-input__inner')))[
            2].send_keys(str(name))

        #点击确定按钮
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,'el-button')))[-1].click()
        sleep(3)

        #断言
        text = WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((By.XPATH,f'//*[text()="{name}"]'))).text
        assert text == name

        #修改刚新增的分组
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located((By.XPATH, '//*[text()="修改"]')))[-1].click()
        sleep(2)

        #输入修改分组名称
        group_name =WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'el-input__inner')))[2]
        group_name.clear()
        group_name.send_keys('11')

        #点击确定按钮
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,'el-button')))[-1].click()
        sleep(3)        # 等待刚新增的分组加上去了

        #删除刚才修改的分组
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located((By.XPATH,'//*[text()="删除"]')))[-1].click()
        sleep(2)

        #点击确认删除按钮
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,'el-button')))[-1].click()

        #等待页面
        sleep(10)






